package vip.xiaozhao.paotui.intf.mapper;

import org.apache.ibatis.annotations.*;
import vip.xiaozhao.paotui.intf.dto.PaoTuiSelectionDO;
import vip.xiaozhao.paotui.intf.entity.PaotuiDetail;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Mapper
public interface PaoTuiDetailMapper {

	@Insert("insert into xzvip_paotui_detail(id,UserId, BringAddressId, TakeAddressId, BringLocation, TakeLocation, CollegeId,`Status`," +
			" VerifyStatus, PayStatus, GoodsType,GoodsWeight, GoodsNum, GoodsInfo,QuHuoCode, ExpectTime, Amount, Fee, Remark, AddTime, UpdateTime, UserIp) " +
			"values(null,#{userId},#{bringAddressId},#{bringAddressEnumId},#{takeAddressId},#{takeAddressEnumId},#{bringLocation},#{takeLocation},#{collegeId},#{status}," +
			" #{verifyStatus}, #{payStatus}, #{goodsType}, #{goodsWeight},#{goodsNum},#{goodsInfo},#{quHuoCode},#{expectTime},#{amount},#{fee},#{remark},now(),now(),#{userIp})")
	@Options(useGeneratedKeys = true, keyColumn = "id", keyProperty = "id")
	int insert(PaotuiDetail detail);

	@Select(" select * from xzvip_paotui_detail where id = #{id} ")
	PaotuiDetail loadById(int id);

	@Select(" select id, status, version from xzvip_paotui_detail where id = #{id} ")
	PaotuiDetail loadVersionLockById(int id);

	@Update({"update xzvip_paotui_detail " +
			"set status = 2 , Version = Version + 1 , updateTime = now()" +
			"where id = #{id}  and Version = #{version} "})
	int updateByVersionLock(int id);

	@Update({"update xzvip_paotui_detail set status = #{status}  , updateTime = now() where id = #{id} "})
	int update(PaotuiDetail paotuiDetail);

	@Select("<script>" +
			"select id,UserId, BringAddressId, TakeAddressId, BringLocation, TakeLocation, CollegeId,`Status`," +
			" VerifyStatus, PayStatus, GoodsType,GoodsWeight, GoodsNum, GoodsInfo,QuHuoCode, ExpectTime, Amount, Fee, Remark, AddTime, UpdateTime, UserIp, UserIp,Amount+Fee as TotalAmount " +
			"From xzvip_paotui_detail " +
			"<where> CollegeId = #{paoTuiSelectionDO.collegeId} AND Status = 1" +
			"<if test='paoTuiSelectionDO.type != 0'> AND GoodsType = #{paoTuiSelectionDO.type}</if>"+
			"<if test='paoTuiSelectionDO.num != 0'> AND GoodsNum = #{paoTuiSelectionDO.num}</if>"+
			"<if test='paoTuiSelectionDO.weight != 0'> AND GoodsWeight = #{paoTuiSelectionDO.weight}</if>"+
			"<if test='paoTuiSelectionDO.time != 5 and paoTuiSelectionDO.time != 0'> AND ExpectTime  &gt;= NOW() AND ExpectTime &lt; #{expectTime}</if>"+
			"<if test='paoTuiSelectionDO.time == 5'> AND ExpectTime &gt; #{expectTime}</if>"+
			"</where>" +
			"<choose>"+
			"<when test='paoTuiSelectionDO.sort == 1'> ORDER BY TotalAmount DESC,id desc</when>"+
			"<when test='paoTuiSelectionDO.sort == 5'> ORDER BY ExpectTime DESC,id desc</when>"+
			"</choose>"+
			"<if test='size != 0'> LIMIT <if test= 'offset != 0'>#{offset},</if> #{size}</if>"+
			"</script>" )
	List<PaotuiDetail> loadListByMultipleConditions(PaoTuiSelectionDO paoTuiSelectionDO, Date expectTime, int offset, int size);

	//查询同一筛选条件下 ，排序相同（金额，添加时间）的记录
	@Select("<script>" +
			"select id,UserId, BringAddressId, TakeAddressId, BringLocation, TakeLocation, CollegeId,`Status`," +
			" VerifyStatus, PayStatus, GoodsType,GoodsWeight, GoodsNum, GoodsInfo,QuHuoCode, ExpectTime, Amount, Fee, Remark, AddTime, UpdateTime, UserIp, UserIp,Amount+Fee as TotalAmount " +
			"From xzvip_paotui_detail " +
			"<where> CollegeId = #{paoTuiSelectionDO.collegeId} AND Status = 1 " +
			"AND id &lt; #{lastId}"+
			"<if test='paoTuiSelectionDO.type != 0'> AND GoodsType = #{type}</if>"+
			"<if test='paoTuiSelectionDO.num != 0'> AND GoodsNum = #{num}</if>"+
			"<if test='paoTuiSelectionDO.weight != 0'> AND GoodsWeight = #{weight}</if>"+
			"<if test='paoTuiSelectionDO.time != 5 and paoTuiSelectionDO.time != 0'> AND ExpectTime  &gt;= NOW() AND ExpectTime &lt; #{expectTime}</if>"+
			"<if test='paoTuiSelectionDO.time == 5'> AND ExpectTime &gt; #{expectTime}</if>"+
			"</where>" +
			"<choose>"+
			"<when test='paoTuiSelectionDO.sort == 1'> AND TotalAmount = #{totalAmount}</when>"+
			"<when test='paoTuiSelectionDO.sort == 5'> AND ExpectTime = #{expectTime}</when>"+
			"</choose>"+
			" ORDER BY id desc "+
			"</script>" )
	List<PaotuiDetail> loadListBySameConditions(PaoTuiSelectionDO paoTuiSelectionDO,int lastId, BigDecimal totalAmount, Date expectTime);

	//查询同一筛选条件下 ，排序（金额，添加时间等）小于最后一条记录 的记录
	@Select("<script>" +
			"select id,UserId, BringAddressId, TakeAddressId, BringLocation, TakeLocation, CollegeId,`Status`," +
			" VerifyStatus, PayStatus, GoodsType,GoodsWeight, GoodsNum, GoodsInfo,QuHuoCode, ExpectTime, Amount, Fee, Remark, AddTime, UpdateTime, UserIp, UserIp,Amount+Fee as TotalAmount " +
			"From xzvip_paotui_detail " +
			"<where> CollegeId = #{paoTuiSelectionDO.collegeId} AND Status = 1 " +
			"<if test='paoTuiSelectionDO.type != 0'> AND GoodsType = #{paoTuiSelectionDO.type}</if>"+
			"<if test='paoTuiSelectionDO.num != 0'> AND GoodsNum = #{paoTuiSelectionDO.num}</if>"+
			"<if test='paoTuiSelectionDO.weight != 0'> AND GoodsWeight = #{paoTuiSelectionDO.weight}</if>"+
			"<if test='paoTuiSelectionDO.time != 5 and paoTuiSelectionDO.time != 0'> AND ExpectTime  &gt;= NOW() AND ExpectTime &lt; #{expectTime}</if>"+
			"<if test='paoTuiSelectionDO.time == 5'> AND ExpectTime &gt; #{expectTime}</if>"+
			"</where>" +
			"<choose>"+
			"<when test='paoTuiSelectionDO.sort == 1'> AND TotalAmount &lt; #{totalAmount}</when>"+
			"<when test='paoTuiSelectionDO.sort == 5'> AND ExpectTime &lt; #{expectTime}</when>"+
			"</choose>"+
			" ORDER BY id desc "+
			"limit #{size} "+
			"</script>" )
	List<PaotuiDetail> loadAfterListByMultipleConditions(PaoTuiSelectionDO paoTuiSelectionDO, BigDecimal totalAmount, Date expectTime,int size);

}
